define(function(require){
    var main = require('/static/main.js');
    require('/static/ajaxfileupload.js');
    require('/static/area/ProJson.js');
    require('/static/area/DistrictJson.js');
    require('/static/area/CityJson.js');
    var init = require('/static/area/init.js');
    var a = new init();
    var m = new main();
    var add = {
        ENTCode :'',
        versionlist : [],
        SysVersionId : '',
        Price:0,
        _init:function(){
            this.bindEvent();
            this.upload();
            this.getversion();
            this.getInfo();
        },
        getversion:function(){
            var _this = this;
            $.ajax({
                type: "POST",
                url: "/enterprise/get-version-list",
                dataType: "json",
                success: function (ret) {
                    var str = '<option value="">选择版本</option>';
                    add.versionlist = ret.data;
                    $.each(ret.Models,function(k,v){
                        str +='<option value="'+ v.SysVersionId+'" data-type="'+ v.SysUseType+'" data-expirydate="'+ v.ExpiryDate+'">'+ v.SysVersionName+'</option>';
                        if(v.SysUseType == 100){
                            $(".SPrice").html(_this.toThousands(v.Price)+'元');
                            _this.Price = v.Price;
                        }
                    });

                    $(".SysVersionId").html(str);
                    setTimeout(function(){
                        $(".SysVersionId").val(add.SysVersionId);
                        add.getVersionCnt();
                    },10);


                }
            });
        },
        getInfo:function(){
            var _this = this;
            $.ajax({
                type: "POST",
                url: "/site/enterprise-detail",
                dataType: "json",
                success: function (ret) {
                    var data = ret.Model;
                    _this.ENTCode = data.ENTCode;
                    _this.setData('ENTLogo',data.ENTLogo);
                    $(".LogoImg img").attr('src',data.ImgHost+data.ENTLogo);
                    $(".IDCardFrontImg img").attr('src',data.ImgHost+data.IDCardFront);
                    $(".IDCardBackImg img").attr('src',data.ImgHost+data.IDCardBack);
                    $(".LicenseImg img").attr('src',data.ImgHost+data.License);
                    _this.setData('ENTName',data.ENTName);
                    _this.setData('ShortName',data.ShortName);
                    _this.setData('CallName',data.CallName);
                    _this.setData('CallPhone',data.CallPhone);
                    _this.setData('LegalPersonName',data.LegalPersonName);
                    _this.setData('IDCardFront',data.IDCardFront);
                    _this.setData('IDCardBack',data.IDCardBack);
                    _this.setData('OpeningBank',data.OpeningBank);
                    _this.setData('ComIndustry',data.ComIndustry);
                    _this.setData('License',data.License);
                    _this.setData('CompanyScale',data.CompanyScale);
                    _this.setData('Address',data.Address);
                    _this.setData('SysVersionId',data.SysVersionId);
                    _this.setData('Cardholder',data.Cardholder);
                    _this.SysVersionId = data.SysVersionId;
                    _this.setData('VersionCnt',data.VersionCnt);
                    $(".SysVersionDescription").html('版本有效期至：'+ m.formatYmd(data.ExpireDate));

                    $(".SCompanyName").html(data.ENTName);
                    $(".SCreateTime").html(m.formatYmd(data.CreateTime));
                    $(".SExpireDate").html(m.formatYmd(data.ExpireDate));
                    a.set(data.ProvinceName,data.CityName,data.AreaName);

                    $(".BankId").val(data.BankId.replace(/(\d{4})/g, "$1 ").trim());
                    _this.setData('Tel',data.Tel.replace(/-/g, ''));
                }
            });
        },
        setData:function(name,data){
            $("#CompanyInfo ."+name).val(data);
        },
        bindEvent:function(){
            var _this = this;
            $(".Submit").click(function(){
                var Tel = $("#AddEnt .Tel").val().trim();
                Tel = Tel.replace(/\s/g, '');
                Tel = Tel.substr(0,4)+'-'+Tel.substr(4,Tel.length);
                var JsonData = {
                    ENTLogo:$("#AddEnt .ENTLogo").val().trim(),
                    ENTName:$("#AddEnt .ENTName").val().trim(),
                    ShortName:$("#AddEnt .ShortName").val().trim(),
                    CallName:$("#AddEnt .CallName").val().trim(),
                    CallPhone:$("#AddEnt .CallPhone").val().trim(),
                    LegalPersonName:$("#AddEnt .LegalPersonName").val().trim(),
                    IDCardFront:$("#AddEnt .IDCardFront").val().trim(),
                    IDCardBack:$("#AddEnt .IDCardBack").val().trim(),
                    Cardholder:$("#AddEnt .Cardholder").val().trim(),
                    OpeningBank:$("#AddEnt .OpeningBank").val().trim(),
                    BankId:$("#AddEnt .BankId").val().replace(/\s/g, ''),
                    ComIndustry:$("#AddEnt .ComIndustry").val().trim(),
                    License:$("#AddEnt .License").val().trim(),
                    CompanyScale:$("#AddEnt .CompanyScale").val().trim(),
                    Province:$("#province").val(),
                    City:$("#citydata").val(),
                    Area:$("#area").val(),
                    Address:$("#AddEnt .Address").val().trim(),
                    Tel:Tel
                }

                if(!_this.seterrorMessage(JsonData.ENTLogo,'请上传企业Logo') ||
                    !_this.seterrorMessage(JsonData.ENTName,'请输入企业名称') ||
                    !_this.seterrorMessage(JsonData.ShortName,'请输入企业简称') ||
                    !_this.seterrorMessage(JsonData.CallName,'请输入联系人姓名') ||
                    !_this.seterrorMessage(JsonData.CallPhone,'请输入联系人手机号')||
                    !m.IsPhone(JsonData.CallPhone)||
                    !_this.seterrorMessage(JsonData.LegalPersonName,'请输入法人姓名')||
                    !_this.seterrorMessage(JsonData.IDCardFront,'请上传法人身份证正面')||
                    !_this.seterrorMessage(JsonData.IDCardBack,'请上传法人身份证反面')||
                    !_this.seterrorMessage(JsonData.OpeningBank,'请输入开户行')||
                    !_this.seterrorMessage(JsonData.Cardholder,'请输持卡人')||
                    !_this.seterrorMessage(JsonData.BankId,'请输入银行卡号')||
                    !m.luhmCheck(JsonData.BankId)||
                    !_this.seterrorMessage(JsonData.ComIndustry,'请选择公司行业')||
                    !_this.seterrorMessage(JsonData.License,'请输入营业执照')||
                    !_this.seterrorMessage(JsonData.CompanyScale,'请选择企业规模')||
                    !_this.seterrorMessage(JsonData.Province,'请选择省') ||
                    !_this.seterrorMessage(JsonData.City,'请选择市') ||
                    !_this.seterrorMessage(JsonData.Area,'请选择区')||
                    !_this.seterrorMessage(JsonData.Address,'请输入地址')||
                    !_this.seterrorMessage(JsonData.Tel,'请输入固定电话')||
                    !m.IsTel(JsonData.Tel)
                ){
                    if(!m.IsPhone(JsonData.CallPhone)){
                        $(".pass-msg.error").html('请填写正确的手机号');
                        return false;
                    }
                    if(!m.luhmCheck(JsonData.BankId)){
                        $(".pass-msg.error").html('请输入正确的银行卡号');
                        return false;
                    }
                    if(!m.IsTel(JsonData.Tel)){
                        $(".pass-msg.error").html('请输入正确的固定电话');
                        return false;
                    }
                    return false;
                }

                JsonData.ProvinceName = $("#province").find("option:selected").text().trim();
                JsonData.CityName = $("#citydata").find("option:selected").text().trim();
                JsonData.AreaName = $("#area").find("option:selected").text().trim();
                JsonData.Tel = JsonData.Tel.replace(/-/g, '');
                if(_this.ENTCode){
                    JsonData.ENTCode = _this.ENTCode;
                }
                $.ajax({
                    type: "POST",
                    data: JsonData,
                    url: "/site/enterprise-info-save",
                    dataType: "json",
                    success: function (ret) {
                        if (ret.IsBizSuccess) {
                            layer.msg('修改成功');
                        } else {
                            layer.msg(ret.ErrMsg);
                        }
                    }
                })
            });
            $('#StartDate1').datetimepicker({
                format:'yyyy-mm-dd',
                language:  'zh-CN',
                minView: "month",
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                forceParse: 0,
                'startDate':new Date(),
            });

            $('.BankId').bind('input propertychange', function() {
                //获取当前光标的位置
                var caret = this.selectionStart
                //获取当前的value
                var value = this.value
                //从左边沿到坐标之间的空格数
                var sp = (value.slice(0, caret).match(/\s/g) || []).length
                //去掉所有空格
                var nospace = value.replace(/\s/g, '');
                //重新插入空格
                var curVal = this.value = nospace.replace(/(\d{4})/g, "$1 ").trim()
                //从左边沿到原坐标之间的空格数
                var curSp = (curVal.slice(0, caret).match(/\s/g) || []).length
            });

            $(".SysVersionId,.VersionCnt").change(function(){
                add.getVersionCnt();
            });
            $(".VersionRenew").click(function(){
                $("#VersionRenew").modal('show');
            });

            $(".PayAccount").change(function(){
                var value = parseInt($(this).val());
                $(".SPrice").html(_this.toThousands(_this.Price *value)+'元');
            });
            $(".add").click(function(){
                  var JsonData = {
                      ENTCode:_this.ENTCode,
                      Count :$(".PayAccount").val()
                  }
                var payway = $("input[name='payway']:checked").val();
                $.ajax({
                    type: "POST",
                    url:'/site/enterprise-version-renew',
                    data:JsonData,
                    dataType: "json",
                    success: function (ret) {
                        if (ret.IsBizSuccess) {
                            //根据支付方式不同跳转到不同的页面
                            if(payway == '100'){
                                window.location.href = "/pay/webpay?orderid="+ret.OrderId;
                            }else{
                                window.location.href = "/pay/alipay?orderid="+ret.OrderId;
                            }

                        } else {
                            layer.msg(ret.ErrMsg);
                        }

                    }
                });

            });

        },
        toThousands:function (num) {

                num = num.toString().replace(/\$|\,/g,'');
                if(isNaN(num))
                    num = "0";
                sign = (num == (num = Math.abs(num)));
                num = Math.floor(num*100+0.50000000001);
                cents = num%100;
                num = Math.floor(num/100).toString();
                if(cents<10)
                    cents = "0" + cents;
                for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                    num = num.substring(0,num.length-(4*i+3))+','+
                    num.substring(num.length-(4*i+3));
                return (((sign)?'':'-') + num + '.' + cents);

        },
        getVersionCnt:function(){
            var Id = $(".SysVersionId").val();
            if(!Id){
                return false;
            }
            Id = Id.trim();
            var Cnt = $(".VersionCnt").val().trim();
            if(Id.length > 0 && Cnt.length > 0){
                Id = parseInt(Id);
                Cnt = parseInt(Cnt);
                if((Id * Cnt) > 0){
                    var type = $(".SysVersionId").find("option:selected").attr('data-type');
                    var ExpiryDate = parseInt($(".SysVersionId").find("option:selected").attr('data-expirydate'));
                    //type = (type == '100' ? '年' : '天');
                    if(type == '200'){
                        var now = new Date();
                        now.setDate(now.getDate()+(ExpiryDate*Cnt));
                        var s = now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate();
                    }else{
                        var now = new Date();
                        var s = (now.getFullYear()+ExpiryDate*Cnt)+"-" + (now.getMonth()+1) + "-" + now.getDate();
                    }
                    //alert(now);
                    $(".SysVersionDescription").html('有效期至：'+s);
                }else{
                    $(".SysVersionDescription").html('　');
                }
            }else{
                $(".SysVersionDescription").html('　');
            }
        },
        upload:function(){
            var type = 0;
            $(document).on("click", ".fileupload", function(){
                $("#file").click();
                type = parseInt($(this).attr("data-type"));
            });
            //图片上传
            $("#file").on("change",function () {
                ajaxFileUpload();
            });
            function ajaxFileUpload(){
                $.ajaxFileUpload({
                    url:m.baseUrl+"/customer/edit-head",
                    secureuri: false,
                    fileElementId: 'file',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            if(type == 100){
                                $(".ENTLogo").val(ret.FileName);
                                $(".LogoImg img").attr('src',ret.Photo);
                            }else if(type == 200){
                                $(".IDCardFront").val(ret.FileName);
                                $(".IDCardFrontImg img").attr('src',ret.Photo);
                            }else if(type == 300){
                                $(".IDCardBack").val(ret.FileName);
                                $(".IDCardBackImg img").attr('src',ret.Photo);
                            }else{
                                $(".License").val(ret.FileName);
                                $(".LicenseImg img").attr('src',ret.Photo);
                            }
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
                return false;
            }
        },
        seterrorMessage:function(data,str){
            if(data.length < 1 || data == ''){
                $(".pass-msg.error").html(str);
                return false;
            }else{
                $(".pass-msg.error").html('');
                return true;
            }
        }
    }
    add._init();
})